/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import javax.swing.table.DefaultTableModel;
import POJO.*;
import DAO.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import sun.management.snmp.jvminstr.JvmCompilationImpl;

/**
 *
 * @author ben
 */
public class form_TaiKhoan extends javax.swing.JInternalFrame {

    /**
     * Creates new form form_TaiKhoan
     */
    public form_TaiKhoan() throws SQLException {
        initComponents();
        
        Load();
    }

    DefaultTableModel model;
    DAO_TaiKhoan tk = new DAO_TaiKhoan();
    DefaultComboBoxModel nvModel;
    DefaultComboBoxModel qModel;

    public void Load() throws SQLException{
        List<POJO_TaiKhoan> list = tk.LoadList();
        model = new DefaultTableModel(null, new Object[]{
            "Tên tài khoản", "Người dùng", "Quyền", "Ngày tạo"
        });
        for(POJO_TaiKhoan taiKhoan : list){
            model.addRow(new Object[]{
                taiKhoan.getTentk(),
                taiKhoan.getManv(),
                taiKhoan.getQuyen(),
                taiKhoan.getNgaytao(),
            });
        };
        tbTaiKhoan.setModel(model);
        
        String a[] = {"Quản lý", "Lái tàu", "Admin"};
        qModel = new DefaultComboBoxModel(a);
        cbbQuyen.setModel(qModel);
        
        DAO_NhanVien nhanVien = new DAO_NhanVien();
        List<POJO_NhanVien> nhanVien1 = nhanVien.LoadList();
        nvModel = new DefaultComboBoxModel(nhanVien1.toArray());
        cbbTenTK.setModel(nvModel);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbTaiKhoan = new javax.swing.JTable();
        cbbTenTK = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        txtMatKhau = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbbQuyen = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        btnThem = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();

        tbTaiKhoan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbTaiKhoan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbTaiKhoanMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbTaiKhoan);

        cbbTenTK.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel1.setText("Tên tài khoản:");

        jLabel2.setText("Mật khẩu:");

        cbbQuyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Quyền:");

        btnThem.setText("Thêm");
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        btnXoa.setText("Xóa");
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnSua.setText("Cập nhật");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnThem)
                        .addGap(82, 82, 82)
                        .addComponent(btnXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(94, 94, 94)
                        .addComponent(btnSua))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbbQuyen, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtMatKhau, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                            .addComponent(cbbTenTK, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(290, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbTenTK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbQuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnSua))
                .addContainerGap(108, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        // TODO add your handling code here:
        if(!txtMatKhau.getText().isEmpty()){
            try {
                POJO_TaiKhoan taiKhoan = new POJO_TaiKhoan();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = new Date();
                
                taiKhoan.setMatkhau(txtMatKhau.getText());
                taiKhoan.setNgaytao(sdf.format(d.getTime()));
                taiKhoan.setQuyen(cbbQuyen.getSelectedItem().toString());
                taiKhoan.setTentk(cbbTenTK.getSelectedItem().toString());
                taiKhoan.setManv(cbbTenTK.getSelectedItem().toString());
                
                tk.Insert(taiKhoan);
                
                Load();
                
                JOptionPane.showMessageDialog(null, "Thêm thành công");
            } catch (SQLException ex) {
                Logger.getLogger(form_TaiKhoan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng nhập mật khẩu");
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void tbTaiKhoanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbTaiKhoanMouseClicked
        // TODO add your handling code here:
        int index = tbTaiKhoan.getSelectedRow();
        qModel.setSelectedItem(String.valueOf(model.getValueAt(index, 2)));
        nvModel.setSelectedItem(String.valueOf(model.getValueAt(index, 1)));
    }//GEN-LAST:event_tbTaiKhoanMouseClicked

    private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        // TODO add your handling code here:
        if(0 <= tbTaiKhoan.getSelectedRow()){
            try {
                POJO_TaiKhoan taiKhoan = new POJO_TaiKhoan();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = new Date();
                
                taiKhoan.setTentk(String.valueOf(model.getValueAt(tbTaiKhoan.getSelectedRow(), 0)));
                
                tk.Delete(taiKhoan);
                Load();
                JOptionPane.showMessageDialog(null, "Xóa thành công");
            } catch (SQLException ex) {
                Logger.getLogger(form_TaiKhoan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else{
            JOptionPane.showMessageDialog(null, "Xin vui lòng chọn đối tượng cần xóa");
        }
    }//GEN-LAST:event_btnXoaActionPerformed

    private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        try {
            // TODO add your handling code here:
            POJO_TaiKhoan taiKhoan = new POJO_TaiKhoan();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = new Date();
            int index = tbTaiKhoan.getSelectedRow();
            
            taiKhoan.setMatkhau(String.valueOf(model.getValueAt(index, 2)));
            taiKhoan.setNgaytao(String.valueOf(model.getValueAt(index, 3)));
            taiKhoan.setQuyen(String.valueOf(cbbQuyen.getSelectedItem()));
            taiKhoan.setTentk(String.valueOf(model.getValueAt(index, 0)));
            taiKhoan.setManv(String.valueOf(model.getValueAt(index, 1)));
            
            tk.Update(taiKhoan);
            Load();
            JOptionPane.showMessageDialog(null, "Cập nhật quyền thành công");
        } 
        catch (SQLException ex) {
            Logger.getLogger(form_TaiKhoan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnSuaActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cbbQuyen;
    private javax.swing.JComboBox cbbTenTK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbTaiKhoan;
    private javax.swing.JTextField txtMatKhau;
    // End of variables declaration//GEN-END:variables
}
